package com.developer.platform.infrastructure.config;

import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;
import static springfox.documentation.builders.RequestHandlerSelectors.withClassAnnotation;

import com.google.common.base.Predicates;
import io.swagger.annotations.Api;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author ：qiaoliang
 * @date ：2020-10-20
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    private final String name = "乔梁";
    private final String url = "http://localhost:9090/";
    private final String email = "1224999091@qq.com";


    @Bean
    public Docket login() {
        return createRestApi(new ApiInfoBuilder()
                .title("登录模块")
                .description("登录接口接口文档")
                .version("0.1")
                .contact(new Contact(name, url, email))
                .termsOfServiceUrl(url)
                .build(),"com.developer.platform.controller.login");
    }

    @Bean
    public Docket system() {
        return createRestApi(new ApiInfoBuilder()
                .title("系统管理模块")
                .description("系统管理接口文档")
                .version("0.1")
                .contact(new Contact(name, url, email))
                .termsOfServiceUrl(url)
                .build(),"com.developer.platform.controller.system");
    }

    @Bean
    public Docket data() {
        return createRestApi(new ApiInfoBuilder()
                .title("数据管理模块")
                .description("数据接口文档")
                .version("0.1")
                .contact(new Contact(name, url, email))
                .termsOfServiceUrl(url)
                .build(),"com.developer.platform.controller.data");
    }

    @Bean
    public Docket monitor() {
        return createRestApi(new ApiInfoBuilder()
                .title("监控管理模块")
                .description("监控接口文档")
                .version("0.1")
                .contact(new Contact(name, url, email))
                .termsOfServiceUrl(url)
                .build(),"com.developer.platform.controller.monitor");
    }


    public Docket createRestApi(ApiInfo apiInfo, String basePackage) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(apiInfo.getTitle())
                .apiInfo(apiInfo)
                .securitySchemes(createSecuritySchemeList())
                .select()
                .apis(Predicates.and(basePackage(basePackage), withClassAnnotation(Api.class)))
                .paths(PathSelectors.any())
                .build();
    }

    private List<SecurityScheme> createSecuritySchemeList(){
        List<SecurityScheme> list = new ArrayList<>();
        list.add(new ApiKey("Authorization", "Authorization", "header"));
        return list;
    }

}
